package com.goldsprite.gameframeworks.screens;

import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;

import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.goldsprite.gameframeworks.utils.math.Vector2;


/**
 * 创建:
 * <br/>- 设置视口, 重写该方法: initViewport(){ setViewport(...) }
 * <br/>- 添加事件处理器: getImp().addInputProcessor(...)
 */
public abstract class GScreen implements IGScreen {
	private final Vector2 viewCenter = new Vector2();
	private final Vector2 viewSize = new Vector2();
	private final Vector2 graphicSize = new Vector2();
	protected ScreenManager screenManager;
	protected InputMultiplexer imp;
	protected boolean initialized = false;
	protected boolean visible = true;
	//绘制底色背景
	private SpriteBatch batch;
	private TextureRegion blackBackground;

	public GScreen() {
	}

	public GScreen(ScreenManager screenManager) {
		this.screenManager = screenManager;
	}

	public void create() {
	}

	@Override
	public void initialize() {
		if (initialized) return;
		init();
		create();
		initialized = true;
	}

	//初始化一些配置
	private void init() {
		batch = new SpriteBatch();
		blackBackground = createColorTextureRegion(Color.valueOf("#222222"));
	}

	@Override
	public boolean isInitialized() {
		return initialized;
	}

	public ScreenManager getScreenManager() {
		return screenManager;
	}

	public void setScreenManager(ScreenManager screenManager) {
		this.screenManager = screenManager;
	}

	public InputMultiplexer getImp() {
		return imp;
	}

	public void setImp(InputMultiplexer imp) {
		this.imp = imp;
	}

	public Viewport getViewport() {
		return getScreenManager().getViewport();
	}

	public Camera getCamera() {
		return getViewport().getCamera();
	}

	public Vector2 getViewSize() {
		viewSize.set(getViewport().getWorldWidth(), getViewport().getWorldHeight());
		return viewSize;
	}

	public Vector2 getViewCenter() {
		viewCenter.set(getViewport().getWorldWidth() / 2, getViewport().getWorldHeight() / 2);
		return viewCenter;
	}

	public Vector2 getGraphicSize() {
		return graphicSize;
	}

	Vector2 tmpCoord = new Vector2();
	public Vector2 screenToWorldCoord(float x, float y) {
		return screenToWorldCoord(tmpCoord.set(x, y));
	}
	public Vector2 screenToWorldCoord(Vector2 screenCoord) {
		// 将反转后的屏幕坐标转换为世界坐标
		Vector3 worldCoordinates = new Vector3(screenCoord.x, screenCoord.y, 0);
		getViewport().unproject(worldCoordinates);
		// 获取转换后的世界坐标
		screenCoord.x = worldCoordinates.x;
		screenCoord.y = worldCoordinates.y;
		return screenCoord;
	}

	public Vector2 worldToScreenCoord(Vector2 worldCoord) {
		// 将反转后的屏幕坐标转换为世界坐标
		Vector3 screenCoordinates = new Vector3(worldCoord.x, worldCoord.y, 0);
		getViewport().project(screenCoordinates);
		// 获取转换后的世界坐标
		worldCoord.x = screenCoordinates.x;
		worldCoord.y = screenCoordinates.y;
		return worldCoord;
	}

	@Override
	public void render(float delta) {
		ScreenUtils.clear(Color.valueOf("#AAAAAA"));

		batch.begin();
		batch.setProjectionMatrix(getCamera().combined);
		batch.draw(blackBackground, 0, 0, getViewSize().x, getViewSize().y);
		batch.end();
	}

	@Override
	public void resize(int width, int height) {
		if (getViewport() != null)
			getViewport().update(width, height, true);
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void show() {
		visible = true;
		getScreenManager().enableInput(getImp());
	}

	@Override
	public void hide() {
		visible = false;
		getScreenManager().disableInput(getImp());
	}

	@Override
	public void dispose() {
	}


	private static TextureRegion createColorTextureRegion(Color color) {
		return new TextureRegion(createColorTexture(color));
	}
	private static Texture createColorTexture(Color color) {
		Pixmap pm = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
		pm.setColor(color);
		pm.fill();
		Texture tex = new Texture(pm);
		pm.dispose();
		return tex;
	}

}
